define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'visiting/index' + location.search,
                    add_url: 'visiting/add',
                    edit_url: 'visiting/edit',
                    del_url: 'visiting/del',
                    import_url: 'visiting/import',
                    multi_url:'visiting/export',
                    table: 'visiting',
                }
            });

            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                //禁用默认搜索
                search: false,
                 //启用普通表单搜索
                commonSearch: true,
                //可以控制是否默认显示搜索单表,false则隐藏,默认为false
                searchFormVisible: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),operate:false},
                        {field: 'name', title: __('Name'),operate:'LIKE'},
                        {field: 'mobile', title: __('Mobile'),operate:false},
                        {field: 'venue', title: __('Venue'),operate:false},
                        {field: 'company', title: __('Company'),operate:'LIKE'},
                        {field: 'department', title: __('政治面貌'),operate:false},
                        {field: 'position_type', title: __('单位类型'),operate:false},
                        {field: 'views', title: __('Views'),operate:false},
                        {field: 'actual_views', title: __('实际人数'),operate:false},
                        {field: 'explain', title: __('是否需要讲解服务'),searchList: {"0": __('不需要'),"1": __('需要')}},
                        {field: 'type', title: __('预约类型'),searchList: {"1": __('团体'),"0": __('个人')}},
                        {
                            field: 'day',
                            title: __('Day'),
                            operate:'RANGE',
                            addclass:'datetimerange',
                        },
                        {field: 'between_time', title: __('Between_time') ,operate:false},
                        {field: 'status', title: __('预约状态'),searchList: {"1": __('预约中'), "4": __('完成参观'), "3": __('取消预约'), "2": __('完成评价'), "5": __('特殊预留'),"6": __('未参观')}},
                        {field: 'code', title: __('预约码'),operate:false},
                        {field: 'actual_time', title: __('到达时间') ,operate:false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}

                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
            // 获取选中项
            $(document).on("click", ".btn-selected", function () {
                var aa = JSON.stringify(table.bootstrapTable('getSelections'));
                if(JSON.stringify(table.bootstrapTable('getSelections')).length > 4300){
                    layer.msg("选择记录数量大于6条,请重新选择");
                    window.reload();
                }
                layer.prompt({
                    formType: 0,
                    value: '',
                    title: '请输入取消原因'
                      },
                    function(value,index){
                    url = "Visiting/send?code="+aa+"&message="+value;
                    $.ajax({
                        type: "get",
                        dataType: "json",
                        url: url,
                        success: function (data) {
                            layer.msg("发送成功");
                            if(data.code == 1){
                                window.location.reload();
                            }
                        }
                    })
                });
            });

            $(document).on("click", ".btn-selectedd", function () {
                var aa = escape(JSON.stringify(table.bootstrapTable('getSelections')));
                if(JSON.stringify(table.bootstrapTable('getSelections')).length > 4300){
                    layer.msg("选择记录数量大于6条,请重新选择");
                    window.reload();
                }
                layer.open({
                    title: '请填写信息',
                    type: 2, //设置为2则可以跳转页面
                    area: ['500px', '300px'],
                    shadeClose: true, //点击遮罩关闭
                    content: "visit/people?code="+aa,
                });

            });


        },
        add: function () {
            Controller.api.bindevent();
        },
        // 获取选中项

        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});